package com.threeSales.controller;

import java.io.IOException;
import java.io.PrintWriter;
import java.util.ArrayList;
import java.util.List;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;

import com.threeSales.entity.Admin;
import com.threeSales.entity.User;
import com.threeSales.services.UserService;
import com.threeSales.utils.JacksonJsonUtil;

@Controller
@RequestMapping("/login")
public class LoginController {
	
	@Autowired
	private UserService userService;
	
/*	@RequestMapping("/checkUser")
	public void checkUser(HttpServletRequest request, HttpServletResponse response) throws Exception{
		int name = Integer.parseInt(request.getParameter("name"));
		String pwd = request.getParameter("pwd");
		String result = userService.findUser(name, pwd);
		String resultJosn = JacksonJsonUtil.beanToJson(result);
		PrintWriter printWriter =response.getWriter();
		printWriter.write(resultJosn);
		printWriter.flush();
		printWriter.close();
	}*/
	
	@RequestMapping("/checkAdmin")
	public void checkAdmin(HttpServletRequest request, HttpServletResponse response) throws Exception{
		int name = Integer.parseInt(request.getParameter("name").trim());
		String pwd = request.getParameter("pwd");
		Admin admin = userService.loginFindAdmin(name, pwd);
		request.getSession().setAttribute("admin", admin);
		String result = userService.findAdmin(name, pwd);
		String resultJosn = JacksonJsonUtil.beanToJson(result);
		PrintWriter printWriter =response.getWriter();
		printWriter.write(resultJosn);
		printWriter.flush();
		printWriter.close();
	}
	
	@RequestMapping("/checkUser")
	public void checkUser(HttpServletRequest request, HttpServletResponse response) throws Exception{
		int name = Integer.parseInt(request.getParameter("name").trim());
		String pwd = request.getParameter("pwd");
		List list = new ArrayList();
		list  = userService.findUser(name, pwd);
		String result="";
		if( list.size()==2){
			  result = (String) list.get(0);
		      User user = (User) list.get(1);
		      request.getSession().setAttribute("user", user);
		}else{
			 result = (String) list.get(0);
		}
		String resultJosn = JacksonJsonUtil.beanToJson(result);
		PrintWriter printWriter =response.getWriter();
		printWriter.write(resultJosn);
		printWriter.flush();
		printWriter.close();
	}

	
	@RequestMapping("/userSessionRemove")
	public void userSessionRemove(HttpServletRequest request,HttpServletResponse response) throws IOException{
		request.getSession().removeAttribute("user");
		response.sendRedirect(request.getContextPath()+"/login.jsp");
	}
	
	@RequestMapping("/adminSessionRemove")
	public void adminSessionRemove(HttpServletRequest request, HttpServletResponse response) throws IOException{
		request.getSession().removeAttribute("admin");
		response.sendRedirect(request.getContextPath()+"/admin.jsp");
	}
	

}
